import {getBaseCategoryList,getBanners,getFloors} from "api/api.js"
export default {
  state:{
    categoryList : [],
    banners:[],
    floors:[]
  },
  getters:{
    filterFloors(state){
      let floors = state.floors
      floors.forEach(item=>{
        if(item.carouselList){
          item.carouselList = item.carouselList.map(({id,imgUrl})=>({
            id,
            url:imgUrl
          }))
        }
      })
      return floors
    }
  },
  mutations:{
    getCategoryList(state,categoryList){
      state.categoryList = categoryList
    },
    getBanners(state,banners){
      state.banners = banners
    },
    getFloors(state,floors){
      state.floors = floors
    }
  },
  actions:{
    async getCategoryList(store){
      try{
        const {data:categoryList,code} = await getBaseCategoryList()
        if(code===200){
          store.commit("getCategoryList",categoryList)
        }else{

        }
        return {categoryList,code}
      }catch(err){
        return Promise.reject(err)
      }
    },
    async getBanners(store){
      try{
        const {data:banners,code} = await getBanners()
        if(code===200){
          store.commit("getBanners",banners)
        }else{

        }
        return {banners,code}
      }catch(err){
        return Promise.reject(err)
      }
    },
    async getFloors(store){
      try{
        const {data:floors,code} = await getFloors()
        if(code===200){
          store.commit("getFloors",floors)
        }else{

        }
        return {floors,code}
      }catch(err){
        return Promise.reject(err)
      }
    }
  }
}